package edu.gatech.teamrocket;

import java.sql.SQLException;

import com.j256.ormlite.dao.Dao;

import edu.gatech.teamrocket.models.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.EditText;

/**
 * The Login form which utilizes login.xml
 * @author Patrick
 */
public class RegisterActivity extends Activity implements OnClickListener, TextWatcher {
	private DewApplication app;
	
	private EditText usernameField;
	private EditText passwordField;
	private EditText passwordVerifyField;
	private Button registerButton;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (DewApplication) getApplicationContext();
		
		setContentView(R.layout.register);
		registerButton = (Button) findViewById(R.id.registerButton);
    	usernameField = (EditText) findViewById(R.id.username);
    	passwordField = (EditText) findViewById(R.id.password);
    	passwordVerifyField = (EditText) findViewById(R.id.passwordVerify);
    	
		registerButton.setOnClickListener(this);
		registerButton.setEnabled(false);
		
		usernameField.addTextChangedListener(this);
		passwordField.addTextChangedListener(this);
		passwordVerifyField.addTextChangedListener(this);
	}

	@Override
	public void onClick(View v) {
		String username = usernameField.getText().toString();
		String password = passwordField.getText().toString();
		String passwordVerify = passwordVerifyField.getText().toString();
		DatabaseHelper db = app.getHelper();
		Dao<User, String> userDao;
		User duplicateUser;
		
		try {
			userDao = db.getUserDao();
			duplicateUser = userDao.queryForId(username);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
		if (!password.equals(passwordVerify)) { // make sure the password fields match
			Toast.makeText(this, "The passwords you entered don't match.", Toast.LENGTH_LONG).show();
		} else if (duplicateUser != null) { // make sure the username is unique
			Toast.makeText(this, "That username's already taken!", Toast.LENGTH_LONG).show();
		} else {
			try {
				User user = new User(username, password); // set up the new user
				userDao.create(user); // save the user to the database
				Dao<Category, Integer> categoryDao = db.getCategoryDao();
				// create some default categories
				categoryDao.create(new Category("Personal", user));
				categoryDao.create(new Category("School", user));
				categoryDao.create(new Category("Work", user));
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
			
			app.getAuth().login(username, password);
    		Toast.makeText(this, "Success! You're logged in now!", Toast.LENGTH_LONG).show();
    		Intent i = new Intent(getApplicationContext(), ListActivity.class);
    		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // remove this activity from history
        	startActivity(i);
        	finish();
		}
	}
	
	public void afterTextChanged(Editable s) {
		// used to make sure all the fields are filled in before allowing the register button
		String username = usernameField.getText().toString();
		String password = passwordField.getText().toString();
		String passwordVerify = passwordVerifyField.getText().toString();
		// check if all the required fields are partially filled out
		if (username.length() > 0 && password.length() > 0 && passwordVerify.length() > 0) {
			registerButton.setEnabled(true);
		} else {
			registerButton.setEnabled(false);
		}
	}
	
	public void beforeTextChanged(Editable s) {	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) { }
}